// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Shares = require('gameshare');
var SceneTypes = Shares.SceneTypes;
var GameEvent = Shares.GameEvent;
var GameObject = Shares.GameObject;
var GameConst = Shares.GameConst

cc.Class({
    extends: cc.Component,

    properties: {
        dir: cc.v2(1, 0),
        speed: 200,
        isPause: true,
        objectType: GameObject.Player
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        cc.zsb.player = this;
        this.lastPos = this.node.position;
        this.lastScreenPos = this.node.convertToWorldSpaceAR(cc.v2(0,0));
        this.screenPos = this.node.convertToWorldSpaceAR(cc.v2(0,0));
        console.log(cc.winSize);
        this.alineToLines();
        this.node.zIndex = 999;
    },

    update (dt) {
        if(this.isPause == false){
            this.lastPos = this.node.position;
            this.lastScreenPos = this.node.convertToWorldSpaceAR(cc.v2(0,0));
            this.node.x += this.speed * this.dir.x * dt;
            this.node.y += this.speed * this.dir.y * dt;
            this.screenPos = this.node.convertToWorldSpaceAR(cc.v2(0,0));
            this.checkWall();
        }
    },
    
    alineToLines: function(){
        var size = GameConst.halfsize;
        var x = this.node.x - size;
        x = Math.floor(x/size/2)*(size * 2) + size;
        var y = this.node.y - size;
        y = Math.floor(y/size/2)*(size * 2) + size;
        this.node.x = x;
        this.node.y = y;
    },

    rotateRight: function(){
        if(this.dir.equals(cc.v2(0, -1))){
            this.dir = cc.v2(-1, 0);
        }
        else if(this.dir.equals(cc.v2(-1, 0))){
            this.dir = cc.v2(0, 1);
        }
        else if(this.dir.equals(cc.v2(0, 1))){
            this.dir = cc.v2(1, 0);
        }
        else if(this.dir.equals(cc.v2(1, 0))){
            this.dir = cc.v2(0, -1);
        }
    },

    checkWall: function(){
        var poso = this.screenPos;
        if(poso.y > cc.winSize.height){
            this.dir = cc.v2(0, -1);
        }
        if(poso.x > cc.winSize.width){
            this.dir = cc.v2(-1, 0);
        }
        if(poso.y < 0){
            this.dir = cc.v2(0, 1);
        }
        if(poso.x < 0){
            this.dir = cc.v2(1, 0);
        }
    },
    
    getScreenPos: function () {
        return this.screenPos;
    },

    getLastScreenPos: function () {
        return this.lastScreenPos;
    },

    fixPos: function(){
        var pos = this.node.position;
        var size = GameConst.size;
        if(this.dir.x != 0){
            this.node.y = Math.floor(pos.y/size)*size + size*0.5;
        }

        if(this.dir.y != 0){
            this.node.x = Math.floor(pos.x/size)*size + size*0.5;
        }
    },

    doAfterCollide: function(node, dir, nodetype){
        var halfsize = GameConst.halfsize;
        if(nodetype && nodetype === GameObject.StaticTrap){
            cc.systemEvent.emit(GameEvent.PlayerRetry);
            return
        }

        var last = this.dir;
        this.node.position = this.lastPos;
        this.dir = dir;
        if(last.add(dir).equals(cc.v2(0,0))){
            this.node.x = node.position.x + halfsize * dir.x;
            this.node.y = node.position.y + halfsize * dir.y
        }
        else{
            this.node.position = node.position;
        }
        //this.fixPos();
    },
    setData: function(data){
        this.node.position = data.position;
        this.dir = cc.v2(data.dir);
    },
    
    toData: function(){
        var data = {
            objectType: this.objectType,
            position: this.node.position,
            dir: this.dir,
        }

        return data;
    },
});
